package database.local;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;
import app.ui.BusinessListArrayAdapter;
import app.ui.PreferencesActivity;

public class PreferencesSyncronizer {
	private List<Business> business_list;
	private PreferencesActivity context;
	private Deal unsync_deal;
	private Likes likes = new Likes();
	private String facebook_user;
	private String facebook_mail;

	public PreferencesSyncronizer(PreferencesActivity context) {
		this.context = context;
	}

	public List<Business> getBusiness_list() {
		return business_list;
	}

	public void setBusiness_list(List<Business> business_list) {
		this.business_list = business_list;
	}

	public class MyAsyncSyncronyser extends AsyncTask<String, Void, Integer> {
		protected Integer doInBackground(String... params) {
			Log.w("Preferences", "Started sync with PreferencesSyncronyser");
			Log.w("Preferences", "Mail = " + params[0]);
			QueryData query = new QueryData();
			query.setUser_email(params[0]);
			// query relevant deals
			business_list = query.getUserData();
			if (business_list == null)
				return 0;

			Log.w("Preferences", "Finished sync with PreferencesSyncronyser");
			return 1;
		}

		protected void onPostExecute(Integer result) {
			Log.i("Preferences",
					"Done getting data, found " + business_list.size()
							+ " business");
			context.business_list = business_list;
			context.myDealsList.setAdapter(new BusinessListArrayAdapter(context, business_list));	
			context.setUserDealsText(business_list.size());
		}

	}

	public class MyAsyncDeleter extends AsyncTask<String, Void, Integer> {
		protected Integer doInBackground(String... params) {
			Log.w("AndroApp", "Started sync with MyAsyncDeleter");
			if (unsync_deal != null) {
				Log.w("AndroApp", "Deal key = " + unsync_deal.getKey());
				try {
					unsync_deal.deleteMe(new User("", params[0]));
					unsync_deal = null;
				} catch (Exception e) {
					unsync_deal = null;
					e.printStackTrace();
				}
			}

			Log.w("AndroApp", "Finished sync with MyAsyncRater");
			return 0;
		}

		protected void onPostExecute(Integer result) {

		}
	}
	
	public class MyAsyncLikerSync extends AsyncTask<String, Void, Integer> {
		protected Integer doInBackground(String... params) {
			Log.w("Preferences", "Started sync with MyAsyncLikerSync");

			try {
				QueryData query = new QueryData();
				likes = query.getUserLikes(new User(facebook_user, facebook_mail));
				if (likes == null)
					return 0;

				Log.w("AndroApp", "Finished sync with MyAsyncLikerSync");
				return 1;
			} catch (Exception e) {
				Log.e("DEBUGTAG", "Preferences likes error", e);
			}

			Log.w("Preferences", "Finished sync with MyAsyncSyncronyser");
			return 0;
		}

		protected void onPostExecute(Integer result) {
			if ((likes.likes_str!=null) && (!likes.likes_str.isEmpty())){
				Log.i("Preferences",
						"Done getting data, found " + likes.likes_str.size()
								+ " likes");
				context.setUserLikesText(likes.likes_str);
				String like_str = "";
				for (String like:likes.likes_int){
					like_str += like + "#";
				}
				Log.i("Preferences", "Like string is: "	+ like_str);
				SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
				Editor editor = prefs.edit();
				editor.putString(PreferencesActivity.PREF_LIKES, like_str);
				editor.commit();
			}
		}

	}
	
	public class MyAsyncLikerSender extends AsyncTask<String, Void, Integer> {
		protected Integer doInBackground(String... params) {
			Log.w("Preferences", "Started sync with MyAsyncLikerSender");

			try {
				Utils.sendFacebookLikes(new User(facebook_user, facebook_mail),likes.likes_str);

				Log.w("AndroApp", "Finished sync with MyAsyncLikerSender");
				return 1;
			} catch (Exception e) {
				Log.e("DEBUGTAG", "Preferences likes error", e);
			}

			Log.w("Preferences", "Finished sync with MyAsyncSender");
			return 0;
		}

		protected void onPostExecute(Integer result) {
			GetLikes(facebook_mail, facebook_user);
		}

	}


	public void GetLikes(String email, String facebook_user) {
		MyAsyncLikerSync inst = new MyAsyncLikerSync();
		this.facebook_user = facebook_user;
		this.facebook_mail = email;
		
		try {
			inst.execute(email);
		} catch (Exception e) {
			Log.e("DEBUGTAG", "Problem with sycronization", e);
		}
	}
	
	public void SendLikes(String email, String facebook_user, List<String> likes) {
		MyAsyncLikerSender inst = new MyAsyncLikerSender();
		this.facebook_user = facebook_user;
		this.facebook_mail = email;
		this.likes.likes_str = likes;
		
		try {
			inst.execute();
		} catch (Exception e) {
			Log.e("DEBUGTAG", "Problem with sycronization", e);
		}
	}
	

	public void sync(String mail) {
		MyAsyncSyncronyser inst = new MyAsyncSyncronyser();
		try {
			inst.execute(mail);
		} catch (Exception e) {
			Log.e("DEBUGTAG", "Problem with sycronization", e);
		}
	}

	public void delete(String mail, Business b) {
		MyAsyncDeleter inst = new MyAsyncDeleter();
		unsync_deal = b.getDeals().get(0);
		try {
			inst.execute(mail);
		} 
		catch (Exception e) {
			Log.e("DEBUGTAG", "Problem with sycronization", e);
		}
	}
}
